package malus.shaofeng.com.beiwomusic.fragment;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.internal.view.menu.ListMenuItemView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import malus.shaofeng.com.beiwomusic.MainActivity;
import malus.shaofeng.com.beiwomusic.R;
import malus.shaofeng.com.beiwomusic.activity.BBSTopicActivity;
import malus.shaofeng.com.beiwomusic.activity.BbsClubActivity;
import malus.shaofeng.com.beiwomusic.adapter.HotPostAdapter;
import malus.shaofeng.com.beiwomusic.model.Forum;
import malus.shaofeng.com.beiwomusic.model.HotPost;
import malus.shaofeng.com.beiwomusic.model.IndexTop;
import malus.shaofeng.com.beiwomusic.utils.BitmapHelper;
import malus.shaofeng.com.beiwomusic.utils.Constants;
import malus.shaofeng.com.beiwomusic.view.ScrollListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainNightSpeakFragment extends Fragment implements View.OnClickListener {
    //推荐
    private IndexTop indexTop;
    private TextView bbsRecommedTitle;
    private TextView bbsRecommedNickname;
    private TextView bbsRecommedCommend;
    private ImageView bbsRecommedBg;
    //窝友俱乐部
    private List<Forum> forums;
    ImageView bbsClubCondition;
    ImageView bbsClubNotice;
    ImageView bbsClubChat;
    ImageView bbsClubPicture;
    //被窝最热30贴
    ListView bbsHotList;
    public MainNightSpeakFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_main_night_speak, container, false);
        forums=new ArrayList<>();
        initView(inflate);
        initData();
        bbsClubCondition.setOnClickListener(this);
        bbsClubNotice.setOnClickListener(this);
        bbsClubChat.setOnClickListener(this);
        bbsClubPicture.setOnClickListener(this);
        return inflate;
    }

    private void initData() {
        HttpUtils httpUtils = new HttpUtils();
        String path="http://kxai.cn/qms/ws/bbs/index.json";
        indexTop=new IndexTop();
        httpUtils.send(HttpRequest.HttpMethod.POST, path, new RequestParams(), new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String result = objectResponseInfo.result.toString();
                initBbsTop(result);
                initBbsClub(result);
                initHotPost(result);
            }
            private void initHotPost(String result) {
                ArrayList<HotPost> data = new ArrayList<>();
                try {
                    JSONObject json  = new JSONObject(result);
                    JSONObject response = json.getJSONObject("response");
                    JSONArray jsonArray = response.getJSONArray("hot_posts");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject object = jsonArray.getJSONObject(i);
                        HotPost hotPost=new HotPost();
                        hotPost.parseJSON(object);
                        data.add(hotPost);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                HotPostAdapter adapter = new HotPostAdapter(getActivity(), data);
                bbsHotList.setAdapter(adapter);

            }
            private void initBbsClub(String result){

                try {
                    JSONObject json = new JSONObject(result);
                    JSONObject response = json.getJSONObject("response");
                    JSONArray objects = response.getJSONArray("forums");

                    for (int i = 0; i <objects.length() ; i++) {
                        JSONObject object = objects.getJSONObject(i);
                        Forum forum=new Forum();
                        forum.parseJSON(object);
                        forums.add(forum);
                    }

                    for (int i=0;i<forums.size();i++) {
                        Forum forum=forums.get(i);
                        if (forum.getImage_uri() != null) {
                            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
                            String url = forum.getImage_uri();
                            String[] split = url.split("/");
                            String path="";
                            for (int j = 3; j < split.length; j++) {
                                path = path+split[j];
                                if(j!=split.length-1){
                                    path+="/";
                                }
                            }
                            path = Constants.URL.IMAGE_URL+path;
                            switch (i){
                                case 0:
                                    bitmapUtils.display(bbsClubCondition, path, new BitmapLoadCallBack<ImageView>() {
                                        @Override
                                        public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                            imageView.setImageBitmap(bitmap);
                                        }

                                        @Override
                                        public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                                            Log.e("asd","asd"+s);

                                        }
                                    });
                                    break;
                                case 1:
                                    bitmapUtils.display(bbsClubPicture, path, new BitmapLoadCallBack<ImageView>() {
                                        @Override
                                        public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                            imageView.setImageBitmap(bitmap);
                                        }

                                        @Override
                                        public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                                            Log.e("asd","asd"+s);

                                        }
                                    });
                                    break;
                                case 2:
                                    bitmapUtils.display(bbsClubNotice, path, new BitmapLoadCallBack<ImageView>() {
                                        @Override
                                        public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                            imageView.setImageBitmap(bitmap);
                                        }

                                        @Override
                                        public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                                            Log.e("asd","asd"+s);
                                        }
                                    });
                                    break;
                                case 3:
                                    bitmapUtils.display(bbsClubChat, path, new BitmapLoadCallBack<ImageView>() {
                                        @Override
                                        public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                            imageView.setImageBitmap(bitmap);
                                        }
                                        @Override
                                        public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                                            Log.e("asd","asd"+s);
                                        }
                                    });
                                    break;
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void initBbsTop(String result) {
                try {
                    bbsRecommedBg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(indexTop.getTopPosts().get(0)!=null){
                                Intent intent = new Intent(getActivity(), BBSTopicActivity.class);
                                intent.putExtra(Constants.BundleKey.KEY_BBS_TOPIC,indexTop.getTopPosts().get(0));
                                startActivity(intent);
                                getActivity().overridePendingTransition(R.anim.alpha_in, 0);
                            }
                        }
                    });
                    JSONObject json = new JSONObject(result);
                    JSONObject response = json.getJSONObject("response");
                    JSONObject index_top = response.getJSONObject("index_top");
                    indexTop.parseJSON(index_top);
                    ArrayList<HotPost> topPosts = indexTop.getTopPosts();
                    HotPost topPost = topPosts.get(0);
                    bbsRecommedTitle.setText(topPost.getTitle().toString());
                    bbsRecommedNickname.setText(topPost.getUser().getNickname());
                    bbsRecommedCommend.setText(topPost.getReplies() + "");
                    if (indexTop.getImage_uri() != null) {
                        BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
                        String url = indexTop.getImage_uri();
                        String[] split = url.split("/");
                        String path="";
                        for (int i = 3; i < split.length; i++) {
                            path = path+split[i];
                            if(i!=split.length-1){
                                path+="/";
                            }
                        }
                        path = Constants.URL.IMAGE_URL+path;

                        bitmapUtils.display(bbsRecommedBg, path, new BitmapLoadCallBack<ImageView>() {
                            @Override
                            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                imageView.setImageBitmap(bitmap);
                            }

                            @Override
                            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("asd", "asdsa");
            }
        });
    }

    private void initView(View inflate) {
        bbsRecommedTitle=(TextView)inflate.findViewById(R.id.bbs_recommend_title);
        bbsRecommedNickname=(TextView)inflate.findViewById(R.id.bbs_recommend_nickname);
        bbsRecommedCommend=(TextView)inflate.findViewById(R.id.bbs_recommend_command);
        bbsRecommedBg=(ImageView)inflate.findViewById(R.id.bbs_recommend_bg);
        bbsClubCondition=(ImageView)inflate.findViewById(R.id.bbs_club_condition);
        bbsClubNotice=(ImageView)inflate.findViewById(R.id.bbs_club_notice);
        bbsClubChat=(ImageView)inflate.findViewById(R.id.bbs_club_chat);
        bbsClubPicture=(ImageView)inflate.findViewById(R.id.bbs_club_picture);
        bbsHotList=(ScrollListView)inflate.findViewById(R.id.bbs_top_hot_30);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(getActivity(), BbsClubActivity.class);
        switch (v.getId()){
            case R.id.bbs_club_condition:
                intent.putExtra(
                        Constants.BundleKey.KEY_BBS_CLUB,
                        Constants.BundleKey.BBS_CLUB_CONDITION);
                break;
            case R.id.bbs_club_picture:
                intent.putExtra(
                        Constants.BundleKey.KEY_BBS_CLUB,
                        Constants.BundleKey.BBS_CLUB_PICTURE);
                break;
            case R.id.bbs_club_notice:
                intent.putExtra(
                        Constants.BundleKey.KEY_BBS_CLUB,
                        Constants.BundleKey.BBS_CLUB_NOTIFY);
                break;
            case R.id.bbs_club_chat:
                intent.putExtra(
                        Constants.BundleKey.KEY_BBS_CLUB,
                        Constants.BundleKey.BBS_CLUB_CHAT);
                break;
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.alpha_in,0);
    }
}
